<template>
	<view class="container">
		<view class="input">
			<u--textarea fixed='true' v-model="content" type='textarea' border='none' maxlength='500' height='180rpx'
				placeholder='说点什么,让缘分来的更快点~' />
		</view>
		<view class="photoList">
			<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="9"
				:maxSize='1024*1024' :previewImage='true'></u-upload>
		</view>
		<view class="publish" v-if="content||fileList.length>0" @click="submit">发表</view>
		<view class="publish" v-else style="background-color: #cccccc;">发表</view>
	</view>
</template>

<script>
	import {
		article
	} from "@/api/article.js"
	import {
		uploadApi_qlw,
		upload_baseUrl
	} from '@/api/baseUrl.js'
	export default {
		data() {
			return {
				content: '',
				fileList: []
			}
		},
		methods: {
			async afterRead(event) {
				console.log('afterRead', event)
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.publishTap(lists[i].thumb)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						image_id: result
					}))
					fileListLen++
				}
				console.log('filelist', this.fileList)
			},
			deletePic(event) {
				console.log('deletePic', event)
				this.fileList.splice(event.index, 1)
			},
			publishTap(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: uploadApi_qlw,
						filePath: url,
						name: 'file',
						formData: {
							'clientname': this.$store.state.clientname
						},
						method: 'POST',
						timeout: 30000,
						header: {
							'Authorization': 'token ' + this.$storage.getStorageDataSync(this.$store.state
								.token),
							'content-type': 'multipart/form-data' // 走网关的接口,一定要加上这个,要不支付宝小程序上传不成功
						},
						success: (uploadFileRes) => {
							var res = JSON.parse(uploadFileRes.data)
							console.log('uploadFileRes', JSON.parse(uploadFileRes.data))
							if (res.code == 10000) {
								// elem.id = res.data.image.id
								// elem.url = upload_baseUrl + res.data.image.fileName
								// that.photos.push(res.data.image.id)
								resolve(res.data.image.id)
							} else {
								uni.showToast({
									title: '上传失败',
									icon: 'none'
								})
							}
						}
					})
				})
			},
			submit() {
				var photos = []
				for (var item of this.fileList) {
					photos.push(item.image_id)
				}
				article.createArticle({
					content: this.content,
					photos: photos
				}).then(res => {
					uni.navigateBack()
					uni.$emit('findKey', {}) //通知find界面更新列表
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		background-color: #FFFFFF;
		padding: 0 30rpx;
	}

	.input {
		padding-top: 20rpx;
	}

	.publish {
		width: 590rpx;
		margin: 100rpx auto;
		border-radius: 10rpx;
		color: #FFFFFF;
		text-align: center;
		font-size: 34rpx;
		height: 88rpx;
		line-height: 88rpx;
		background-image: linear-gradient(to right, #e02700, #df7d16);
	}
</style>
